package garin.parse.sql.sql2003;

import garin.parse.Token;

import java.io.IOException;

public class StatementEndToken extends Token {
	public StatementEndToken(final Token parent) {
		super(parent);
	}
	
	@Override
	public boolean accept() throws IOException {
		return source().is(";");
	}
	
	private Token nextStatement() throws Exception {
		exit();
		Token p = parent();
		while (!(p instanceof SqlStatement))
			p = p.parent();
		final SqlStatement stmt = (SqlStatement) p;
		stmt.children.clear();
		return stmt;
	}
	
	@Override
	public Token on() throws Exception {
		if (is(";"))
			return self();
		else
			return nextStatement();
	}
}
